package com.shuangyu.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shuangyu.common.R;
import com.shuangyu.dto.DishDto;
import com.shuangyu.entity.Dish;
import com.shuangyu.entity.DishFlavor;
import com.shuangyu.mapper.DishMapper;
import com.shuangyu.service.DishFlavorService;
import com.shuangyu.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 新增菜品，同时保存对应的口味数据
     * @param dishDto
     */
    @Transactional      //多表操作需要加这个“事务控制”，加这个要在主程序里加上@EnableTransactionManagement这个注解才能用
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品
        this.save(dishDto);

        //因为flavors里的flavor没有dishId，所以要设置下dishId
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());

        //批量保存口味数据
        dishFlavorService.saveBatch(flavors);

    }

    /**
     * 删除菜品及对应的口味数据
     * @param ids
     */
    @Transactional
    public void removeWithFlavor(String ids) {
        String[] idss = ids.split(",");

        for(String id: idss){
            this.removeById(id);

            LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(DishFlavor::getDishId, id);
            dishFlavorService.remove(queryWrapper);
        }

    }

    /**
     * 根据id查询菜品的信息
     * @param id
     * @return
     */
    @Transactional
    public DishDto getWithFlavorById(String id) {
        DishDto dishDto = new DishDto();

        Dish dish = this.getById(id);
        BeanUtils.copyProperties(dish, dishDto);

        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, id);
        List<DishFlavor> list = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(list);

        return dishDto;
    }

    /**
     * 更新菜品信息
     * @param dishDto
     */
    @Transactional
    public void updateWithFlavorById(DishDto dishDto) {
        this.updateById(dishDto);

        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());

        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(dishFlavorLambdaQueryWrapper);

        dishFlavorService.saveBatch(dishDto.getFlavors());

    }


}
